package com.iraeta.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.iraeta.HikConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "HikCameraApi")
public class HikCameraApi {
    private static final Logger logger = LoggerFactory.getLogger(HikCameraApi.class);

    @Autowired
    private HikConfig hikConfig;

    public final String  ARTEMIS_PATH = "/artemis";

    public JSONArray getCamera(String regionIndexCode) throws Exception{
        JSONArray res = new JSONArray();
        int pageNo = 1;
        while(true){
            JSONArray jsonArray = this.getCameraLoop(regionIndexCode, pageNo);
            res.addAll(jsonArray);
            pageNo+=1;
            if(jsonArray.size()<1000){
                break;
            }
        }
        return res;
    }
    private JSONArray getCameraLoop(String regionIndexCode,int pageNo) throws Exception {
        String url = ARTEMIS_PATH + "/api/resource/v2/camera/search";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("isSubRegion", true);
        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", 1000);
        jsonBody.put("regionIndexCodes",  new String[]{regionIndexCode});
        String body = jsonBody.toJSONString();
        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[camera/search]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[camera/search]错误:" + jsonResult.getString("msg"));
        }
        JSONObject jsonResultData = jsonResult.getJSONObject("data");
        return jsonResultData.getJSONArray("list");
    }

    public String getCameraPreviewHls(String indexCode) throws Exception{
        return this.getCameraPreview(indexCode, "hls");
    }
    public String getCameraPreviewRtmp(String indexCode) throws Exception{
        return this.getCameraPreview(indexCode, "rtmp");
    }
    public String getCameraPreviewWs(String indexCode) throws Exception{
        return this.getCameraPreview(indexCode, "ws");
    }
    private String getCameraPreview(String indexCode, String protocol) throws Exception{
        String url = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", url); //Key 为协议，Value 为接口路径
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("protocol", protocol);
        jsonBody.put("cameraIndexCode",  indexCode);
        String body = jsonBody.toJSONString();

        String result = ArtemisHttpUtil.doPostStringArtemis(new ArtemisConfig(hikConfig.getHost(), hikConfig.getAppkey(), hikConfig.getAppsecret()),
                path, body, null, null,"application/json;charset=UTF-8");
        if(StrUtil.isEmpty(result)){
            throw new RuntimeException("调用HIK接口[camera/previewURLs]未知错误");
        }
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if(!code.equals("0")){
            throw new RuntimeException("调用HIK接口[camera/previewURLs]错误:" + jsonResult.getString("msg"));
        }
        JSONObject jsonResultData = jsonResult.getJSONObject("data");
        return jsonResultData.getString("url");
    }
}
